<?

	require_once "_MemberFunc.php";

	function &Code_GetSelect($type, $level, $code, $getAll = true )
	{
		$db = GetDefaultConnection();
		global $s_member_level;
		$select  = " SELECT CODE_SEQ, CODE_CODE1, CODE_CODE2, CODE_NAME, CODE_DISPLAY ";
		$from  = " FROM TB_CODE ";

		$where .= " WHERE CODE_TYPE = '$type'   ";
		if ($level == 1)
			$where .= " and CODE_CODE2=0 ";
		elseif ($level == 2)
			$where .= " and CODE_CODE1=$code and CODE_CODE2>0 ";

		if ( !$getAll )
			$where .= " and del_yn='N' ";

		$orderby = " ORDER BY CODE_DISPLAY ASC";

		$sql = $select . $from . $where . $orderby;

		return $db->queryList($sql);
	}
	function Code_PrintOption($inidata, $level, $idx=0, $ex="")
	{
		if ($inidata->getLength() != 0)
		{
			while ($inidata->hasNext())
			{
				$row = $inidata->next();

				if ($level==1)
					$seq = $row->getNumber('CODE_CODE1');
				else
				{
					if ($ex=="KG")
						$seq = $row->getNumber('CODE_NAME');
					else
						$seq = $row->getNumber('CODE_CODE2');
				}
				echo "<option value='". $seq ."' ";
				if ($seq==$idx)
					echo "selected";
				echo ">".$row->getString('CODE_NAME')."</option>";
			}
		}
	}
	//농협선택
	function &JBNH_GetSelect($seq)
	{
		$db = GetDefaultConnection();
		$memberinfo = GetCurrentUserInfo();
		$s_member_level = $memberinfo["userlvl"];
		$s_member_id = $memberinfo["userid"];

		if ($s_member_level==1)
		{
			$sql = "SELECT JBNH_SEQ, JBNH_NAME, JBNH_REGDATE FROM TB_JBNH ";
			if ($seq != 0)
				$sql .= " WHERE JBNH_SEQ = $seq ";
			else
				$sql .= " WHERE DEL_YN != 'Y' ";
		}
		else if ($s_member_level==2)
		{
			$sql = "select distinct a.JBNH_SEQ, b.JBNH_NAME from TB_MANAGER a inner join TB_JBNH b ";
			$sql .= "where a.DEL_YN != 'Y'  and a.JBNH_SEQ=b.JBNH_SEQ and a.MEMBER_ID='$s_member_id' order by b.JBNH_NAME ASC";
			if ($seq != 0)
				$sql .= " WHERE a.JBNH_SEQ = $seq ";
		}
		else if ($s_member_level==3)
		{
			$sql = "select distinct a.JBNH_SEQ, b.JBNH_NAME from TB_MEMBER a inner join TB_JBNH b ";
			$sql .= "where a.DEL_YN != 'Y'  and a.JBNH_SEQ=b.JBNH_SEQ and a.MEMBER_ID='$s_member_id'";
			if ($seq != 0)
				$sql .= " WHERE a.JBNH_SEQ = $seq ";
		}

		return $db->queryList($sql);
	}

	function JBNH_PrintOption($inidata, $idx=0)
	{
		if ($inidata->getLength() != 0)
		{
			while ($inidata->hasNext())
			{
				$row = $inidata->next();
				echo "<option value='".$row->getNumber('JBNH_SEQ')."' ";
				if ($row->getNumber('JBNH_SEQ')==$idx)
					echo "selected";
				echo ">".$row->getString('JBNH_NAME')."</option>";
			}
		}
	}

	function Order_InsertArticle($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_ORDER";

		$date = date("Y-m-d 00:00:00");
		$sql = "SELECT count(ORDER_SEQ) AS cnt FROM $table_name where ORDER_REGDATE>='$date'";
		$row =& $db->queryRow($sql);
		$order_code = $row->getNumber('cnt') + 1;
		$order_code = date(ymd)."-". right("000". $order_code,4);

		$now = date('Y-m-d H:i:s');
		$array['ORDER_CODE'] = $order_code;
		$array['ORDER_REGDATE'] = $now;

		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}

	function Order_UpdateArticle($array, $seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_ORDER";
		$where = " ORDER_SEQ=".$seq;

		$result = $db->queryUpdate($table_name, $array, $where);

		if (!$result)
			return -1;
		else
			return 1;
	}
	function Order_DeleteArticle($seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_ORDER";
		$where = " ORDER_SEQ=".$seq;

		$result = $db->queryDelete($table_name, $where);

		if (!$result)
			return -1;
		else
			return 1;
	}

	function right($value, $count)
	{
		$value = substr($value, (strlen($value) - $count), strlen($value));
		return $value;
	}

	function &Common_GetList($tableName, $queryarry, $orderby)
	{
		$db = GetDefaultConnection();
		global $s_member_level;

		$select  = " SELECT * FROM $tableName ";
		$where = " WHERE 1=1 ";

		$query=makeWhereQry($queryarry);

		if ($query != "")
			$where .= " and ". $query;

		$sql = $select . $where . $orderby;

		return $db->queryList($sql);
	}

	function &Code_GetNhFarmSelect($code)
	{
		$db = GetDefaultConnection();
		global $s_member_level;
		$sql  = " SELECT * FROM TB_FARM WHERE JBNH_SEQ = $code order by FARM_SEQ";

		return $db->queryList($sql);
	}

	function &Code_GetFItemSelect($code)
	{
		$db = GetDefaultConnection();
		global $s_member_level;
		$sql  = "SELECT TB_FARM.FARM_ITEMCODE1, TB_CODE.CODE_NAME FROM TB_FARM inner join TB_CODE WHERE TB_FARM.FARM_ITEMCODE1=TB_CODE.CODE_CODE1 and TB_CODE.CODE_CODE2=0 and TB_CODE.CODE_TYPE='A' and TB_FARM.FARM_SEQ = $code";

		return $db->queryList($sql);
	}

	function TempProduct_InsertArticle($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT_TEMP";

		$now = date('Y-m-d H:i:s');
		$array['T_PRODUCT_REGDATE'] = $now;

		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}

	function Product_InsertArticle($array, $tseq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT";

		$now = date('Y-m-d H:i:s');
		$array['PRODUCT_REGDATE'] = $now;

		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}

	function TempProduct_UpdateArticle($array, $seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT_TEMP";
		$where = " T_PRODUCT_SEQ=".$seq;

		$result = $db->queryUpdate($table_name, $array, $where);

		if (!$result)
			return -1;
		else
			return 1;
	}
	function Product_UpdateArticle($array, $seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT";
		$where = " T_PRODUCT_SEQ=".$seq;

		$result = $db->queryUpdate($table_name, $array, $where);

		if (!$result)
			return -1;
		else
			return 1;
	}
	function TempProduct_DeleteArticle($seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT_TEMP";
		$where = " T_PRODUCT_SEQ=".$seq;

		$result = $db->queryDelete($table_name, $where);

		if (!$result)
			return -1;
		else
			return 1;
	}
	function Product_DeleteArticle($seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT";
		$where = " T_PRODUCT_SEQ=".$seq;

		$result = $db->queryDelete($table_name, $where);

		if (!$result)
			return -1;
		else
			return 1;
	}
	function ResultProduct_InsertArticle($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT_RESULT";

		$now = date('Y-m-d H:i:s');
		$array['R_PRODUCT_REGDATE'] = $now;

		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
		{
			$db->rollback();
			return -1;
		}
		else
		{
			$sql ="update TB_PRODUCT_TEMP set T_PRODUCT_TRADE=T_PRODUCT_TRADE+". $array['R_PRODUCT_TOTAL'] ." where T_PRODUCT_SEQ=". $array['T_PRODUCT_SEQ'];
			$result = $db->query($sql);

			if (!$result)
			{
				$db->rollback();
				return -1;
			}
			else
				return 1;
		}
	}
	function ResultProduct_UpdateArticle($array, $seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT_RESULT";
		$where = " R_PRODUCT_SEQ=".$seq;

		$sql = "select R_PRODUCT_TOTAL from $table_name where $where";
		$row =& $db->queryRow($sql);
		$rtot = $array['R_PRODUCT_TOTAL']-$row->getNumber('R_PRODUCT_TOTAL');

		$result = $db->queryUpdate($table_name, $array, $where);
		if (!$result)
		{
			$db->rollback();
			return -1;
		}
		else
		{
			$sql ="update TB_PRODUCT_TEMP set T_PRODUCT_TRADE=T_PRODUCT_TRADE+". $rtot ." where T_PRODUCT_SEQ=". $array['T_PRODUCT_SEQ'];
			$result = $db->query($sql);

			if (!$result)
			{
				$db->rollback();
				return -1;
			}
			else
				return 1;
		}
	}
	function ResultProduct_DeleteArticle($seq, $seq2)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_PRODUCT_RESULT";
		$where = " R_PRODUCT_SEQ=".$seq2;

		$sql = "select R_PRODUCT_TOTAL from $table_name where $where";
		$row =& $db->queryRow($sql);
		$rtot = $row->getNumber('R_PRODUCT_TOTAL');

		$result = $db->queryDelete($table_name, $where);
		if (!$result)
		{
			$db->rollback();
			return -1;
		}
		else
		{
			$sql ="update TB_PRODUCT_TEMP set T_PRODUCT_TRADE=T_PRODUCT_TRADE-". $rtot ." where T_PRODUCT_SEQ=". $seq;
			$result = $db->query($sql);

			if (!$result)
			{
				$db->rollback();
				return -1;
			}
			else
				return 1;
		}
	}
	function Trade_InsertArticle($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_TRADE";

		$now = date('Y-m-d H:i:s');
		$array['TRADE_REGDATE'] = $now;

		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
		{
			$db->rollback();
			return -1;
		}
		else
		{
			$sql ="update TB_PRODUCT set PRODUCT_TRADE=PRODUCT_TRADE+". $array['TRADE_NUMBER'] ." where PRODUCT_SEQ=". $array['PRODUCT_SEQ'];
			$result = $db->query($sql);

			if (!$result)
			{
				$db->rollback();
				return -1;
			}
			else
				return 1;
		}
	}
	//login check
	require_once "_CheckLogin.php";
?>